Java8 Stream的分组,分区

package com.lgx.jdk8.part02;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * Stream的分组,分区
 */
public class Test10StreamGroupBy {
    public static void main(String[] args) {
        User user1 = new User("zhangsan", 60, 20);
        User user2 = new User("lisi", 80, 23);
        User user3 = new User("zhangsan", 80, 24);
        User user4 = new User("wangwu", 50, 24);

        List<User> userList = Arrays.asList(user1, user2, user3, user4);

        //按照名字分组
        Map<String, List<User>> nameMap = userList.stream().collect(Collectors.groupingBy(User::getName));
        System.out.println("nameMap = " + nameMap);

        //按照分数分组
        Map<Integer, List<User>> scoreMap = userList.stream().collect(Collectors.groupingBy(User::getScore));
        System.out.println("scoreMap = " + scoreMap);

        //按照名字分组,并求出个数
        Map<String, Long> nameMap2 = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.counting()));
        System.out.println("nameMap2 = " + nameMap2);

        //按照名字分组,分数平局值
        Map<String, Double> nameMap3 = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.averagingDouble(User::getScore)));
        System.out.println("nameMap3 = " + nameMap3);

        /**分区:分区是分组的一种特殊情况,分组可以分成多组,分区只分成2组**/
        //及格的人
        Map<Boolean, List<User>> map4 = userList.stream().collect(Collectors.partitioningBy(user -> user.getScore() >= 60));
        System.out.println("map4 = " + map4);



    }
}

class User{
    private String name;
    private int score;
    private int age;
    public User(String name, int score, int age) {
        this.name = name;
        this.score = score;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", age=" + age +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值